package ext.tianma.project2.APQP.controller;

import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.org.WTUser;
import wt.projmgmt.admin.Project2;
import wt.query.QuerySpec;
import wt.session.SessionHelper;
import wt.session.SessionServerHelper;
import wt.util.WTException;
import wt.util.WTProperties;

import com.ptc.projectmanagement.plan.PlanActivity;

import ext.tianma.project2.APQP.constants.APQPProjectConstant;
import ext.tianma.project2.APQP.service.APQPPlanActivityService;
import ext.tianma.project2.APQP.service.APQPProjectService;
import ext.tianma.project2.APQP.service.ProcessService;
import ext.tianma.project2.util.ProjectUtil;
import ext.tianma.util.TMUtil;

/**
 * 类名: APQPController </br>
 * 包名: ext.tianma.project2.APQP.controller
 * 描述: APQP Controller类
 * @author xucr
 * 创建时间: 2020年8月13日
 * 发布版本: V1.0
 */

@Controller
@RequestMapping({ "/APQPController" })
public class APQPController {
	
	@Autowired
	APQPProjectService projectservice;
	
	@Autowired
	APQPPlanActivityService planactivityservice;
	
	@Autowired
	ProcessService processservice;
	
	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(APQPController.class);
	

	/**
	 * 创建APQP项目
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月13日
	 */
	@RequestMapping(value = "/createProject2", method = {RequestMethod.POST, RequestMethod.GET })
	public void createProject2(
			@RequestBody(required = false) String params,
			HttpServletRequest request, HttpServletResponse response
			) throws Exception {
		TMUtil.setHeader(response);
		logger.info("createProject2 >>>> params：" + params);
		response.setContentType("text/html;charset=UTF-8");
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try {
			PrintWriter out = response.getWriter();
			JSONObject result = projectservice.createAPQP(params);
			out.print(result.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 创建/查看项目时，展示活动
	 * @param params
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月18日
	 */
	@RequestMapping(value = "/showPlanInfo", method = {RequestMethod.POST, RequestMethod.GET })
	public void showPlanInfo(@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		logger.info("showPlanInfo >>>> params：" + params);
		try {
			JSONObject result = null;
			JSONObject obj = new JSONObject(params);
			if(obj.has("projectOid")) {
				String projectOid = obj.getString("projectOid");
				Project2 project = (Project2) TMUtil.oid2Obj(projectOid);
				result = planactivityservice.getPlanActivityView(project);
			} else {
				String modelName = obj.getString("modelName");
				obj.put("modelName", modelName);
				QuerySpec qs = ProjectUtil.queryAPQPsByParams(obj, APQPProjectConstant.APQP_PROJECT);
				QueryResult qr = PersistenceHelper.manager.find(qs);
				if(qr.size() > 0) {
					result = new JSONObject();
					result.put("msg", APQPProjectConstant.TIP5);
					result.put("code", "1");
				} else {
					result = planactivityservice.initPlanGrid(modelName);
				}
			}
			out.print(result.toString());
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 查询枚举
	 * @param params
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月18日
	 */
	@RequestMapping(value = "/getEnumeration", method = { RequestMethod.POST, RequestMethod.GET })
	public void getEnumeration(
			@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		logger.info("getEnumeration >>>> params：" + params);
		try {
			JSONObject obj = new JSONObject(params);
			String enumerName = obj.getString("enumerName");
			
			JSONObject ob = ProjectUtil.getEnumeration(enumerName);
			
			out.print(ob.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 获取群组成员
	 * @param groupName
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月18日
	 */
	@RequestMapping(value = "/getGroupMember", method = { RequestMethod.POST, RequestMethod.GET })
	public void getGroupMember(
			@RequestParam(value = "groupName", required = false) String groupName,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject ob = ProjectUtil.getGroupMember(groupName);
			out.print(ob.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 查询机型名称
	 * @param partNumber
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月20日
	 */
	@RequestMapping(value = "/getModelName", method = { RequestMethod.POST, RequestMethod.GET })
	public void getModelName(
			@RequestParam(value = "partNumber", required = false) String partNumber,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject ob = projectservice.getModelName(partNumber);;
			out.print(ob.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 搜索PM
	 * @param userName
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年9月8日
	 */
	@RequestMapping(value = "/getUserByName", method = { RequestMethod.POST, RequestMethod.GET })
	public void getUserByName(
			@RequestParam(value = "userName", required = false) String userName,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try {
			List<WTUser> v =  ProjectUtil.getUserList("*" + userName + "*");
			JSONArray array = new JSONArray();
			if(null != v && v.size() > 0) {
				JSONObject obj = null;
				for(int i = 0 ; i < v.size(); i++) {
					obj = new JSONObject();
					WTUser u = (WTUser) v.get(i);
					obj.put("userName", u.getName());
					obj.put("userFullName", u.getFullName());
					array.put(obj);
				}
			}
			JSONObject rsult = ProjectUtil.respInfo(array, array.length());
			out.print(rsult.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 按条件搜索APQP
	 * @param params
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月24日
	 */
	@RequestMapping(value = "/queryAPQPs", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryAPQPs(
			@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		logger.info("queryAPQPs >>>> params：" + params);
		PrintWriter out = response.getWriter();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		JSONObject ob = null;
		try {
			
			ob = projectservice.queryAPQPs(params);
			out.print(ob.toString());
		} catch (WTException e) {
			ob = new JSONObject();
			e.printStackTrace();
			logger.error("queryAPQPs >>>catch >>>>>: " + e.getLocalizedMessage(Locale.getDefault()));
			ob.put("msg", e.getLocalizedMessage(Locale.getDefault()));
			ob.put("code", "1");
			out.print(ob.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 查看项目详情
	 * @param projectOid
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月20日
	 */
	@RequestMapping(value = "/findAPQP", method = { RequestMethod.POST, RequestMethod.GET })
	public void findAPQP(
			@RequestParam(value = "projectOid", required = false) String projectOid,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try {
			WTUser u = (WTUser) SessionHelper.manager.getPrincipal();
			JSONObject ob = projectservice.findAPQP(projectOid, u, APQPProjectService.READPROJECTTYPE[0]);
			out.print(ob.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 查询活动详情
	 * @param projectOid
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月20日
	 */
	@RequestMapping(value = "/findPlanActivity", method = { RequestMethod.POST, RequestMethod.GET })
	public void findPlanActivity(
			@RequestParam(value = "planActivityOid", required = false) String planActivityOid,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject ob = planactivityservice.findPlanActivity(planActivityOid);
			out.print(ob.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 上载报告
	 * @param projectOid
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月20日
	 */
	@RequestMapping(value = "/upLoadRepoet", method = { RequestMethod.POST, RequestMethod.GET })
	public void upLoadRepoet(
			@RequestParam(value="attachfile", required = false ) MultipartFile attachfile,
			@RequestParam(value = "planActivityOid", required = false) String planActivityOid,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		JSONObject result = new JSONObject();
		PrintWriter out = response.getWriter();
		try {
			logger.info("upLoadRepoet >> planActivityOid : " + planActivityOid);
			PlanActivity planactivity = (PlanActivity) TMUtil.oid2Obj(planActivityOid);
			
			FileItem item = ((CommonsMultipartFile) attachfile).getFileItem();
			String filename = item.getName();
			WTProperties props = WTProperties.getLocalProperties();
			String filePath = props.getProperty("wt.codebase.location") + File.separator + "temp" + File.separator + "APQP" + File.separator + "upload";
			File targerFile = new File(filePath);
			if(!targerFile.exists()) {
				targerFile.mkdirs();
			}
			InputStream in = item.getInputStream();
			FileUtils.copyInputStreamToFile(in, new File(filePath + File.separator + filename));
			
			Project2 project = (Project2) planactivity.getContainer();
			planactivityservice.createOrUpdateReportDoc(project, planactivity, filename, filePath, "");
			
			//报告上载完成后，将任务变为：已上载
			JSONObject attrs = new JSONObject();
			attrs.put("isUpload", APQPProjectConstant.YES);
			planactivityservice.setPlanactivitySTDAttr(planactivity, attrs);
			
			result.put("code", "0");
			result.put("msg", "");
			
			out.print(result.toString());
		} catch (WTException e) {
			e.printStackTrace();
			logger.error("upLoadRepoet >>>catch >>>>>: " + e.getLocalizedMessage(Locale.getDefault()));
			result.put("msg", e.getLocalizedMessage(Locale.getDefault()));
			result.put("code", "1");
			out.print(result.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * SQE启用任务（上载报告后，将任务启用：变为已达标）
	 * @param projectOid
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月20日
	 */
	@RequestMapping(value = "/completePlanActivity", method = { RequestMethod.POST, RequestMethod.GET })
	public void completePlanActivity(
			@RequestParam(value = "planActivityOid", required = false) String planActivityOid,
			HttpServletResponse response) throws Exception {
		/**
		 * 这里的完成任务，并非是关闭任务，
		 * 而是修改属性【是否达标 isStandard】的值为yes，变为已达标
		 * */
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		JSONObject result = new JSONObject();
		PrintWriter out = response.getWriter();
		try {
			PlanActivity planactivity = (PlanActivity) TMUtil.oid2Obj(planActivityOid);
			
			JSONObject attrs = new JSONObject();
			attrs.put("isStandard", APQPProjectConstant.YES);
			planactivityservice.setPlanactivitySTDAttr(planactivity, attrs);
			
			//判断是否能够发起流程
			Project2 project = (Project2) planactivity.getContainer();
			
			boolean isClosedProject = projectservice.isClosedProject(project);
			if(isClosedProject) {
				//没有数据，启动流程，保存表单
//				WfProcess process = processservice.createProcess(
//						APQPProjectConstant.APQP_WFPROCESSTEMPLATE_NAME, APQPProjectConstant.APQP_LIFECYCLETEMPLATE_NAME, project);
//				processservice.setFormDataXml(process, project);
				
				//TO-BE:直接关闭
				processservice.closedProject(project);
			}
			
			result.put("code", "0");
			result.put("msg", "");
			
			out.print(result.toString());
		} catch (WTException e) {
			e.printStackTrace();
			logger.error("completePlanActivity >>>catch >>>>>: " + e.getLocalizedMessage(Locale.getDefault()));
			result.put("msg", e.getLocalizedMessage(Locale.getDefault()));
			result.put("code", "1");
			out.print(result.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 删除报告
	 * @param params
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年9月2日
	 */
	@RequestMapping(value = "/deleteRepoet", method = { RequestMethod.POST, RequestMethod.GET })
	public void deleteRepoet(
			@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		PrintWriter out = response.getWriter();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		JSONObject result = new JSONObject();
		logger.info("deleteRepoet >>>> params：" + params);
		try {
			JSONObject obj = new JSONObject();
			JSONObject paramsObj = new JSONObject(params);
			if(paramsObj.has("planActivityOid") && paramsObj.has("deliveryOid")) {
				String planActivityOid = paramsObj.getString("planActivityOid");
				String deliveryOid = paramsObj.getString("deliveryOid"); 
				PlanActivity planactivity = (PlanActivity) TMUtil.oid2Obj(planActivityOid);
				boolean isTrue = planactivityservice.deleteDeliverySubject(deliveryOid);
				if(isTrue) {
					//报告删除后，将任务变为：未上载、未达标
					obj.put("isUpload", APQPProjectConstant.NO);
					obj.put("isStandard", APQPProjectConstant.NO);
					planactivityservice.setPlanactivitySTDAttr(planactivity, obj);
					
					result.put("code", "0");
					result.put("msg", "");
				}
			} else {
				result.put("code", "1");
				result.put("msg", "删除异常，请联系系统管理员确认报告数据是否正常");
			}
			out.print(result.toString());
		} catch (WTException e) {
			e.printStackTrace();
			logger.error("upLoadRepoet >>>catch >>>>>: " + e.getLocalizedMessage(Locale.getDefault()));
			result.put("msg", e.getLocalizedMessage(Locale.getDefault()));
			result.put("code", "1");
			out.print(result.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 修订
	 * @param planActivityOid
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年8月31日
	 */
	@RequestMapping(value = "/revise", method = { RequestMethod.POST, RequestMethod.GET })
	public void revise(
			@RequestParam(value = "planActivityOid", required = false) String planActivityOid,
			HttpServletResponse response) throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		Map<String, Object> result = new HashMap<String, Object>();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		logger.info("revise >>>> planActivityOid：" + planActivityOid);
		PrintWriter out = response.getWriter();
		try {
			String tip = planactivityservice.revise(planActivityOid);
			result.put("msg", tip);
			result.put("code", StringUtils.isEmpty(tip) ? 0 : 1);
			out.print(result.toString());
		} catch (WTException e) {
			e.printStackTrace();
			logger.error("revise >>>catch >>>>>: " + e.getLocalizedMessage(Locale.getDefault()));
			result.put("msg", e.getLocalizedMessage(Locale.getDefault()));
			result.put("code", "1");
			out.print(result.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 导出项目列表
	 * @param params
	 * @param response
	 * @return
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年9月3日
	 */
	@RequestMapping(value = "/export", method = { RequestMethod.POST, RequestMethod.GET })
	public void export(
			@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		/**
		 * 导出逻辑
		 * 1、当未勾选数据时，传入搜索条件，按照条件导出
		 * 2、当勾选数据后，传入勾选的项目oid，
		 * */
		
		logger.info("export >>>> params：" + params);
		response.setContentType("text/html;charset=UTF-8");
		TMUtil.setHeader(response);
		JSONObject result = new JSONObject();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		try {
			if(StringUtils.isNotEmpty(params)) {
				JSONObject obj = new JSONObject(params);
				JSONObject form = obj.getJSONObject("form");//搜索条件
				JSONArray selected = obj.getJSONArray("selected");//选择导出的项目oid
				
				QuerySpec qs = null;
				if(null != selected && selected.length() > 0) {
					String ids = "";
					JSONObject queryParams = new JSONObject();
					for(int i = 0 ; i < selected.length(); i++) {
						ids += selected.getString(i) + APQPProjectConstant.SPLITVALUE;
					}
					queryParams.put("ids", ids);
					qs = ProjectUtil.queryAPQPsByParams(queryParams, APQPProjectConstant.APQP_PROJECT);
				} else {
					qs = ProjectUtil.queryAPQPsByParams(form, APQPProjectConstant.APQP_PROJECT);
				}
				
				if(null != qs) {
					QueryResult qr = PersistenceHelper.manager.find(qs);
					JSONArray array = new JSONArray();
					WTUser u = (WTUser) SessionHelper.manager.getPrincipal();
					Object [] o = null;
					List<Project2> projects = new ArrayList<Project2>();
					while (qr.hasMoreElements()) {
						o = (Object []) qr.nextElement();
						Project2 project = (Project2) o[0];
						if(!projects.contains(project)) {
							array.put(projectservice.findAPQP(project, u , APQPProjectService.READPROJECTTYPE[2]));
							projects.add(project);
						}
					}
					
					String path = projectservice.exportExcel(array);
					result.put("code", "0");
					result.put("msg", "");
					result.put("path", path);
				}
			} else {
				result.put("code", "1");
				result.put("msg", "导出异常，请确认数据是否正常");
			}
			out.print(result.toString());
		} catch (WTException e) {
			e.printStackTrace();
			logger.error("export >>>catch >>>>>: " + e.getLocalizedMessage(Locale.getDefault()));
			result.put("msg", e.getLocalizedMessage(Locale.getDefault()));
			result.put("code", "1");
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 获取物料类别
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年9月29日
	 */
	@RequestMapping(value = "/getMaterialCategory", method = { RequestMethod.POST, RequestMethod.GET })
	public void getMaterialCategory(HttpServletResponse response) throws Exception {
		TMUtil.setHeader(response);
		response.setContentType("text/html;charset=UTF-8");
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try {
			PrintWriter out = response.getWriter();
			JSONObject result = projectservice.getMaterialCategory();
			out.print(result.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 同步料号
	 * @param projectOid
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年9月29日
	 */
	@RequestMapping(value = "/synchronization", method = { RequestMethod.POST, RequestMethod.GET })
	public void synchronization(
		@RequestParam(value = "projectOid", required = false) String projectOid,
		HttpServletResponse response) throws Exception {
		
		TMUtil.setHeader(response);
		response.setContentType("text/html;charset=UTF-8");
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try {
			Project2 project = (Project2) TMUtil.oid2Obj(projectOid);
			PrintWriter out = response.getWriter();
			
			//查询当前APQP已有料号
			List<String> historyPN = new ArrayList<String>();
			JSONObject params = new JSONObject();
			params.put("hierarchy", "L1");
			String pn = ProjectUtil.getQueryName("pn", APQPProjectConstant.APQP_PLANACTIVITY);
			QuerySpec qs = ProjectUtil.queryPlanActivityByParams(project, params);
			int p = qs.getFromClause().getPosition(PlanActivity.class);
			qs.appendSelectAttribute(pn, p, false);
			
			QueryResult qr = PersistenceHelper.manager.find(qs);
			Object [] obj = null;

			while(qr.hasMoreElements()) {
				obj = (Object[]) qr.nextElement();
				String _pn = (String) obj[1];
				historyPN.add(_pn);
			}
			
			//当前APQP下，BOM(机型名称)新增料号
			JSONObject result = planactivityservice.initPlanGrid(project, historyPN);
			out.print(result.toString());
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

}
